package com.pesking.core.sqlparser.processor;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.pesking.core.parameter.Parameter;
import com.pesking.core.sqlparser.parameter.SqlParameter;
import com.pesking.core.sqlparser.parameter.SqlParameterProcessor;
import com.pesking.core.tenant.MultiTenantManage;
import com.pesking.core.tenant.domain.TenantInfoDomain;

/**
 * 租户名称赋值
 * 
 * @author cypei
 * @since 2021-01-25
 */

public class TenantNameSqlParameterHandler extends SqlParameterProcessor {

	@Override
	public void handle(StringBuilder sbSQL, Parameter parameter, List<SqlParameter> processors,SqlParameter sqlParameter) {
		String name = sqlParameter.getName();
		String value = null;
		if (!StringUtils.isBlank(name)) {
			value = parameter.getParameter(name);
		}

		if (StringUtils.isBlank(value)) {
			TenantInfoDomain tid = MultiTenantManage.getCurrentTenantInfo();
			value = "" + tid.getName();
		}
		sbSQL.append(value);
	}

	@Override
	public List<String> getSqlParameterType() {
		return Arrays.asList(new String []{"TENANT_NAME"});
	}
}
